﻿using System.Collections.Generic;
using System.Linq;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Business
{
    public partial class Business
    {
        #region -- Get Subject --

        /// <summary>
        /// This function shows list of Subject Required  by Subject.
        /// </summary>
        /// <param name="idSubject"></param>
        /// <returns></returns>
        public List<SubjectRequire> GetListSubjectRequireBySubject(int idSubject)
        {
            return DataContex != null && idSubject > 0
                     ? DataContex.SubjectRequires.Where(sr => sr.IdSubject == idSubject).ToList()
                     : null;
        }

        #endregion

        #region Common

        private static bool SubjectRequireCommonValidator(SubjectRequire subjectRequire, out string message)
        {
            if (subjectRequire.IdSubject <= 0 || subjectRequire.IdSubjectRequire <= 0 || subjectRequire.IdSubject == subjectRequire.IdSubjectRequire)
            {
                message = string.Format(ConstantMessages.CanNotIdentifyItem);
                return false;
            }

            message = string.Empty;
            return true;
        }

        #endregion

        #region --Add Subject Require --

        /// <summary>
        /// This function checks validate of add a new subject require into a subject
        ///     Is subject exsit?
        ///     Is new subject require same as orther subject
        /// </summary>
        /// <param name="subjectRequire"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddNewSubjectRequireValidator(SubjectRequire subjectRequire, out string message)
        {
            if (!SubjectRequireCommonValidator(subjectRequire, out message)) return false;

            if (DataContex.SubjectRequires.Any(sr => sr.IdSubject == subjectRequire.IdSubject && sr.IdSubjectRequire == subjectRequire.IdSubjectRequire))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "này");
                return false;
            }

            return true;
        }

        /// <summary>
        /// This function adds new a major in a facutly
        /// </summary>
        /// <param name="subjectrRequire"></param>
        public void AddNewSubjectRequire(SubjectRequire subjectrRequire)
        {
            DataContex.SubjectRequires.AddObject(subjectrRequire);
            DataContex.SaveChanges();
        }

        #endregion

        #region --Delete Subject--

        /// <summary>
        /// This fuction checks validate of delete a subject require fuction
        /// </summary>
        /// <param name="subjectRequires"></param>
        /// <param name="message"></param>
        /// <returns></returns>       
        public bool DeleteSubjectRequireValidator(IEnumerable<SubjectRequire> subjectRequires, out string message)
        {
            const string intialize = "Môn học tiên quyết: ";
            var itemsCannotIdentify = (from subjectRequire in subjectRequires
                                       let id = subjectRequire.Id
                                       where !DataContex.SubjectRequires.Any(s => s.Id == id)
                                       select subjectRequire).Aggregate(intialize, (current, subjectRequire) => current + (subjectRequire.SubjectName + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }           

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subjectRequires"></param>
        public void DeleteSubjectRequires(List<SubjectRequire> subjectRequires)
        {
            for (var i = subjectRequires.Count - 1; i >= 0; i--)
            {
                var id = subjectRequires[i].Id;
                var deleteSubjectRequire = DataContex.SubjectRequires.Single(s => s.Id == id);

                DataContex.SubjectRequires.DeleteObject(deleteSubjectRequire);
            }
            DataContex.SaveChanges();
        }

        #endregion        
    }
}